package com.board.controller;


import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.board.dto.OrderCommentDto;
import com.board.dto.StoreReplyDto;
import com.board.entity.OrderComment;
import com.board.form.FindStoreCommentForm;
import com.board.form.StoreCommentReplyForm;
import com.board.form.UserCommentForm;
import com.board.form.UserFindCommentForm;
import com.board.param.FindStoreCommentParam;
import com.board.param.StoreCommentReplyParam;
import com.board.param.UserCommentParam;
import com.board.param.UserFindCommentParam;
import com.board.service.OrderCommentService;
import com.board.util.JwtUtil;
import com.board.util.PageInfo;
import com.board.util.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiModelProperty;
import io.swagger.annotations.ApiOperation;
import io.swagger.v3.oas.annotations.Parameter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Pattern;

/**
 * <p>
 * 膳食系统-订单评价表 前端控制器
 * </p>
 *
 * @author 小叶
 * @since 2024年04月04日
 */
@RestController
@RequestMapping("/orderComment")
@Api(tags = "订单评价接口")
public class OrderCommentController {
    @Autowired
    private OrderCommentService orderCommentService;

    /**
     * 用户评价订单
     * @param form 评价的内容，包含评价等级，评价内容，是否举报等
     * @param token 用户token
     * @return 返回评价详情
     */
    @PostMapping("/userCommentOrder")
    @ApiOperation("用户评价订单")
    private Result userCommentOrder(@RequestBody UserCommentForm form, @RequestHeader String token){
        Integer userId = (Integer) JwtUtil.getData(token, "subject");
        UserCommentParam param = BeanUtil.toBean(form, UserCommentParam.class);
        OrderCommentDto orderComment= orderCommentService.userCommentOrder(param,userId);
        return Result.success("用户评价订单成功",orderComment);
    }

    /**
     * 用户删除评价
     * @param commentId 要删除的评价id
     * @param token 用户token信息
     * @return 返回被删除的数据
     */
    @PostMapping("/userDelComment")
    @ApiOperation("用户删除评价")
    public Result userDelComment(
                                      Integer commentId, @RequestHeader String token){
        Integer userId = (Integer) JwtUtil.getData(token, "subject");
        OrderCommentDto dto = orderCommentService.userDelComment(commentId,userId);
        return Result.success("用户删除评价成功",dto);
    }

    /**
     * 商家回复评论
     * @param form 回复的评论id和内容
     * @param token 用户token
     * @return 返回评论的内容
     */
    @PostMapping("/storeComment")
    @ApiOperation("商家回复评论")
    public Result storeComment(@RequestBody StoreCommentReplyForm form, @RequestHeader String token){
        Integer storeId = (Integer) JwtUtil.getData(token, "store_id");
        StoreReplyDto dto = orderCommentService.storeComment(BeanUtil.toBean(form, StoreCommentReplyParam.class),storeId);
        return Result.success("商家回复评论成功",dto);
    }

    /**
     * 根据id查询评价
     * @param id 要查询的评价id
     * @return 返回查询的信息
     */
    @GetMapping("/ssfindCommentById")
    @ApiOperation("根据id查询评价和回复")
    public Result findCommentById(Integer id){
        OrderCommentDto pageInfo =  orderCommentService.findCommentById(id);
        return Result.success("查询成功",pageInfo);
    }

    /**
     * 用户条件查询自己的评价
     * @param form 查询条件，包含是否满意，是否带图，页码，页数
     * @param token 查询的用户token
     * @return 返回查询的评价
     */
    @GetMapping("/userFindComment")
    @ApiOperation("用户条件查询自己的所有评价")
    public Result userFindComment(UserFindCommentForm form, @RequestHeader String token){
        Integer userId = (Integer) JwtUtil.getData(token, "subject");
        PageInfo<OrderCommentDto> pageInfo= orderCommentService.userFindComment(BeanUtil.toBean(form, UserFindCommentParam.class),userId);
        return Result.success("用户查询评价成功",pageInfo);
    }

    /**
     * 条件查询某个商家的评价
     * @param form 查询条件，包含商家id，是否满意，是否带图，页码，页数
     * @return
     */
    @GetMapping("/storeFindComment")
    @ApiOperation("条件查询某个商家的评价")
    public Result storeFindComment(FindStoreCommentForm form){
        PageInfo<OrderCommentDto> pageInfo= orderCommentService.storeFindComment(BeanUtil.toBean(form, FindStoreCommentParam.class));
        return Result.success("查询成功",pageInfo);
    }

}

